﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Windows.Forms;
using System.Threading;

namespace etimet
{
    class AppConfiguration
    {
        /// <summary>
        /// all supported languages, that have available resource file.
        /// hard coded values, for now
        /// </summary>
        static public string SupportedLanguages = "en,he";
        
        /// <summary>
        /// Holds the current language for all forms
        /// </summary>
        public string CurrentLang;

        /// <summary>
        /// the configuration file stream
        /// </summary>
        static private string filename = "Configuration.xml";

        /// <summary>
        /// 
        /// </summary>
        public AppConfiguration()
        {
            try
            {
                XmlDocument doc = new XmlDocument();

                doc.Load(filename);

                XmlNodeList lst = doc.GetElementsByTagName("option");
                foreach (XmlNode it in lst)
                {
                    if (it.Attributes.GetNamedItem("name").Value.Equals("CurrentLanguage"))
                    {
                        CurrentLang = it.InnerXml;
                    }
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void StoreConfigOption(string option, string value)
        {
            // taken from http://www.java2s.com/Code/CSharp/XML/AppendChild.htm

            XmlDocument doc = new XmlDocument();
            doc.Load(filename);

            XmlNode root = doc.DocumentElement;

            // first, drop the existing option
            XmlNodeList lst = doc.GetElementsByTagName("option");
            XmlNode old = null;
            foreach (XmlNode it in lst)
            {
                if (it.Attributes.GetNamedItem("name").Value.Equals(option))
                {
                    old = it;
                }
            }
            if (old == null)
            {
                // we didn't find the option in the xml
                throw new ApplicationException("Could not find the option in the configuration XML file. Sorry!");
            }
            else
            {
                old.ParentNode.RemoveChild(old);
            }

            // second, insert new option instead
            XmlNode newOp = doc.CreateElement("option");
            XmlAttribute att = doc.CreateAttribute("name");
            att.Value = option;
            newOp.Attributes.Append(att);
            root.AppendChild(newOp);
            newOp.AppendChild(doc.CreateTextNode(value));

            doc.Save(filename);
        }

        ~AppConfiguration()
        {
            
        }
    }
}
